package com.school.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.school.demo.model.auto.ReturnMessage;
import com.school.demo.model.auto.School;
import com.school.demo.service.ISchoolService;
import com.school.demo.util.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Charlotte
 * @since 2021-02-28
 */
@Api(tags = "学校管理")
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private ISchoolService schoolService;


    @ApiOperation(value = "获取学校列表", notes = "", tags = {})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数据起始", name = "offset", paramType = "query", example = "0"),
            @ApiImplicitParam(value = "数据条数", name = "limit", paramType = "query", example = "10")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    @JsonView(ReturnMessage.ItemList.class) 指定返回ItemList类型的值
    public ReturnMessage<School> getList(HttpServletRequest request, HttpServletResponse response, @Param("schoolName") String schoolName, @Param("schoolCity") String schoolCity) {

        try {
            QueryWrapper<School> queryWrapper = new QueryWrapper<>();
            if (!schoolName.isEmpty()) {
                queryWrapper.lambda().like(School::getSchoolName, schoolName);
            }
            if (!schoolCity.isEmpty()) {
                queryWrapper.lambda().like(School::getSchoolCity, schoolCity);
            }
            List<School> list = this.schoolService.list(queryWrapper);
            ReturnMessage<School> msg = new ReturnMessage<>(list);
            msg.setMsg(Status.SUCCESS.message);
            msg.setCode(Status.SUCCESS.code);
            return msg;
        } catch (Exception e) {
            ReturnMessage<School> msg = new ReturnMessage<>();
            msg.setMsg(e.getMessage());
            msg.setCode(Status.WARN.code);
            return msg;
        }
    }


    @ApiOperation(value = "学校详情", tags = {})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "ID", name = "id", paramType = "path", example = "0")
    })
    //@JsonView({ CardView.class })
    @RequestMapping(value = "/more/{id}", method = RequestMethod.POST)
    public @ResponseBody
    ReturnMessage<School> get(HttpServletRequest request,
                              @PathVariable("id") String id, HttpServletResponse response) {

        School school = schoolService.getById(id);
        ReturnMessage<School> msg = new ReturnMessage<>();
        msg.setItem(school);
        msg.setMsg(Status.SUCCESS.message);
        msg.setCode(Status.SUCCESS.code);
        return msg;
    }

    //@ApiOperation(value = "Save Card", tags = { })
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody
    void save(HttpServletRequest request, @RequestBody School school,
              HttpServletResponse response) {//, @PathVariable("language") String language

        int id = school.getId();
        if (id != -1) {
            School dbItem = this.schoolService.getById(id);
            school = dbItem;
        }
        this.schoolService.save(school);
    }

    //@ApiOperation(value = "Save Card", tags = { })
    //@ApiImplicitParams({
    //		@ApiImplicitParam(value = "记录Id", name = "id", paramType = "query", example = "1234567890abcdef1234567890abcdef")
    //})
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public void delete(HttpServletRequest request, @PathVariable("id") String id, HttpServletResponse response) {
        this.schoolService.removeById(id);
    }
}
